EEPROM interfacing with Arduino
/* Name : main.c
* Purpose : Source code for External EEPROM Interfacing with Arduino.
* Author : Gemicates
* Date : 10-02-2018
* Website : www.gemicates.org
* Revision : None
*/
#include <LiquidCrystal.h>
#include <Wire.h>
LiquidCrystal lcd(6, 7, 8, 9, 10, 11); // initialize the LCD library with the numbers of the interface pins
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data )
{
int rdata = data;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(rdata);
Wire.endTransmission();
}
void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length )
{
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddresspage >> 8)); // MSB
Wire.write((int)(eeaddresspage & 0xFF)); // LSB
byte c;
for ( c = 0; c < length; c++)
Wire.write(data[c]);
Wire.endTransmission();
}
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress )
{
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length )
{
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,length);
int c = 0; // the variable which holds the data which is read from the eeprom
for ( c = 0; c < length; c++ )
if (Wire.available()) buffer[c] = Wire.read();
}
void setup()
{
char somedata[] = "GEMICATES"; // data to write
Wire.begin();
Serial.begin(9600); // initialize the serial port with baud rate 9600
i2c_eeprom_write_page(0x50, 0, (byte *)somedata, sizeof(somedata)); // write to EEPROM
delay(10); //add a small delay
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.cursor();
lcd.print(somedata); // prints DATA which is stored before
}
void loop()
{
int addr=0; //first address
byte b = i2c_eeprom_read_byte(0x50, 0); // access the first address from the memory
while (b!=0)
{
Serial.print((char)b); //print content to serial port
addr++; //increase address
b = i2c_eeprom_read_byte(0x50, addr); // access the first address from the memory
}
Serial.println();
delay(200); //add delay
}